package bar.dark.lee;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.format.Time;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import bar.dark.lee.common.BDUtil;
import bar.dark.lee.setting.data.DataOutActivity;

public class IntroPasswordActivity extends SuperActivity
{
	public static final String MODE = "mode";
	
	/** 패스워드 입력 에디트 텍스트 */
	private EditText passwordEdit1;
	private EditText passwordEdit2;
	private EditText passwordEdit3;
	private EditText passwordEdit4;
	
	/** 패스워드 입력 버튼 */
	private Button passwordButton;
	
	/** 사용자 패스워드 값 */ 
	private String password = "";
	
	/** 사용자 입력 패스워드 값*/
	private String inputPassword = "";
	
	private String [] passwords = new String[4];
	
	private int modeFlag;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		isAdFlag = false;
		
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.intro_password);
		
		setTitle("비밀번호 입력");
		setTextTip("백업");
		
		modeFlag = getIntent().getIntExtra(MODE, 0);
		
		if (modeFlag == 1)
		{
			setGoneTipButton();
		}
		
		setFindView();
		setOnClickListener();
		setData();
		
		Handler handler = new Handler(new Handler.Callback()
		{
			@Override
			public boolean handleMessage(Message msg)
			{
				InputMethodManager mgr = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
				mgr.showSoftInput(passwordEdit1, InputMethodManager.SHOW_IMPLICIT);
				return false;
			}
		});
		
		handler.sendMessageDelayed(new Message(), 300);
	}
	
	private void setFindView()
	{
		passwordEdit1 = (EditText) findViewById(R.id.introPassword1);
		passwordEdit2 = (EditText) findViewById(R.id.introPassword2);
		passwordEdit3 = (EditText) findViewById(R.id.introPassword3);
		passwordEdit4 = (EditText) findViewById(R.id.introPassword4);
		//passwordButton = (Button) findViewById(R.id.introPasswordButton);
	}
	
	private void setOnClickListener()
	{
		passwordEdit1.addTextChangedListener(new TextWatcher()
		{
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count)
			{
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after)
			{
			}
			
			@Override
			public void afterTextChanged(Editable s)
			{
				if (!s.toString().equals(""))
				{
					passwordEdit2.requestFocus();
					passwords[0] = s.toString();
				}
			}
		});
		
		passwordEdit2.addTextChangedListener(new TextWatcher()
		{
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count)
			{
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after)
			{
			}
			
			@Override
			public void afterTextChanged(Editable s)
			{
				if (!s.toString().equals(""))
				{
					passwordEdit3.requestFocus();
					passwords[1] = s.toString();
				}
			}
		});
		
		passwordEdit3.addTextChangedListener(new TextWatcher()
		{
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count)
			{
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after)
			{
			}
			
			@Override
			public void afterTextChanged(Editable s)
			{
				if (!s.toString().equals(""))
				{
					passwordEdit4.requestFocus();
					passwords[2] = s.toString();
				}
			}
		});
		
		passwordEdit4.addTextChangedListener(new TextWatcher()
		{
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count)
			{
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after)
			{
			}
			
			@Override
			public void afterTextChanged(Editable s)
			{
				if (!s.toString().equals(""))
				{
					passwords[3] = s.toString();
					
					inputPassword = "";
					for (int i = 0; i < passwords.length; i++)
					{
						inputPassword += passwords[i]; 
					}
					
					if (inputPassword.equals(password))
					{
						if (modeFlag == 1)
						{
							goCancel();
						}
						else
						{
							goIntroActivity();
						}
					}
					else
					{
						passwordEdit1.setText("");
						passwordEdit2.setText("");
						passwordEdit3.setText("");
						passwordEdit4.setText("");

						passwordEdit1.requestFocus();
						
						BDUtil.showToast(IntroPasswordActivity.this, "비밀번호가 틀렸습니다.");
					}
				}
			}
		});
		
		passwordEdit2.setOnKeyListener(new OnKeyListener()
		{
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event)
			{
				if (keyCode == KeyEvent.KEYCODE_DEL)
				{
					EditText et = (EditText) v;
					if (et.getText().toString().equals(""))
					{
						passwordEdit1.setText("");
						passwordEdit1.requestFocus();
					}
				}
				
				return false;
			}
		});
		
		passwordEdit3.setOnKeyListener(new OnKeyListener()
		{
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event)
			{
				if (keyCode == KeyEvent.KEYCODE_DEL)
				{
					EditText et = (EditText) v;
					if (et.getText().toString().equals(""))
					{
						et.setText("");
						passwordEdit2.requestFocus();
					}
				}
				
				return false;
			}
		});
		
		passwordEdit4.setOnKeyListener(new OnKeyListener()
		{
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event)
			{
				if (keyCode == KeyEvent.KEYCODE_DEL)
				{
					EditText et = (EditText) v;
					if (et.getText().toString().equals(""))
					{
						et.setText("");
						passwordEdit3.requestFocus();
					}
				}
				
				return false;
			}
		});
		
//		passwordButton.setOnClickListener(new OnClickListener()
//		{
//			@Override
//			public void onClick(View v)
//			{
//				Intent intent = new Intent(IntroPasswordActivity.this, DataOutActivity.class);
//				startActivity(intent);
//			}
//		});
	}

	private void setData()
	{
		SharedPreferences pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
		int passwordSetting = pref.getInt(BDConstents.PASSWORD_SETTING, 0);
		password = pref.getString(BDConstents.PASSWORD, "");
		
		if (passwordSetting < 1)
		{
			goIntroActivity();
		}
	}
	
	private void goIntroActivity()
	{
		Intent intent = new Intent(this, IntroActivitiy.class);
		startActivity(intent);
		finish();
	}
	
	private void goCancel()
	{
		Intent intent = new Intent();
		setResult(Activity.RESULT_OK, intent);
		finish();
	}
	
	@Override
	protected void tipButton()
	{
		Intent intent = new Intent(IntroPasswordActivity.this, DataOutActivity.class);
		startActivity(intent);
	}
}
